package com.yunkaweilai.android.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.yunkaweilai.android.R;
import com.yunkaweilai.android.interfaces.UpdateGoodsNumInterfaces;
import com.yunkaweilai.android.model.shop.SearchShopModel;
import com.yunkaweilai.android.utils.MoneyTextWatcher;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.MyToast;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2018/7/4.
 */

public class UpdateGoodsNumDialogForSearch extends Dialog {

    private SearchShopModel.DataBean.GoodsListBean shopSkuInfoModel;
    private UpdateGoodsNumInterfaces updateGoodsNumInterfaces;
    private EditText idEdtNum;
    private TextView TextTitle;
    private String title;

    public UpdateGoodsNumDialogForSearch(@NonNull Context context, SearchShopModel.DataBean.GoodsListBean shopSkuInfoModel, UpdateGoodsNumInterfaces updateGoodsNumInterfaces, String title) {
        super(context, R.style.dialog);
        this.shopSkuInfoModel = shopSkuInfoModel;
        this.title = title;
        this.updateGoodsNumInterfaces = updateGoodsNumInterfaces;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_update_goods_num);
        idEdtNum = ((EditText) findViewById(R.id.id_edt_num));
        TextTitle = (TextView)findViewById(R.id.id_tv_max_type);
        TextTitle.setText(title);
//        idEdtNum.addTextChangedListener(new MoneyTextWatcher(idEdtNum));
        findViewById(R.id.id_tv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        findViewById(R.id.id_tv_right).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isEmpty(idEdtNum.getText().toString())) {
                    MyToast.showShortToast(getContext(), "还未输入数量");
                } else {
                    shopSkuInfoModel.setBuy_num(Double.parseDouble(idEdtNum.getText().toString()));
                    updateGoodsNumInterfaces.refreshGoods();
                    dismiss();
                }

            }
        });
        idEdtNum.addTextChangedListener(new MoneyTextWatcher(idEdtNum));
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                InputMethodManager inputManager = (InputMethodManager) idEdtNum.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(idEdtNum, 0);
            }
        }, 800);
    }
}
